<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 500px;
            margin: 100px auto;
        }

        .title {
            width: 100%;
            text-align: center;
            background-color: #CCCCCC;
            padding: 10px 0;
            font-size: 25px;
            font-weight: bold;
        }

        input {
            outline: none;
            width: calc(100% - 4px);
            height: 40px;
            font-size: 17px;
            text-indent: 1em;
            padding: 0;
            margin: 0;
            margin-top: 10px;
        }

        table {
            width: 100%;
            margin-top: 10px;
            text-align: center;
            border-style: none;
            border-color: #CCCCCC;
            border-collapse: collapse;
        }

        table td {
            padding: 15px 0;
            margin: 0;
            border-color: #CCCCCC;
            border-style: solid;
        }

        table thead {
            font-weight: bolder;
        }

        .noneData {
            color: red;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="title">
            商品信息查询
        </div>
        <input type="text" placeholder="搜索商品">
        <table border="1">
            <thead>
                <tr>
                    <td>名称</td>
                    <td>价格</td>
                    <td>库存</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>白菜</td>
                    <td>1元/斤</td>
                    <td>100斤</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        let $ = str => {
            let dom = document.querySelectorAll(str);
            if (dom.length == 1) {
                return dom[0];
            }
            return dom;
        }

        let data = [{
                name: "白菜",
                price: 1,
                stock: 100
            },
            {
                name: "土豆",
                price: 1.5,
                stock: 600
            },
            {
                name: "番薯",
                price: 2,
                stock: 120
            },
            {
                name: "荷兰豆",
                price: 4,
                stock: 300
            },
            {
                name: "豆角",
                price: 5,
                stock: 500
            },
            {
                name: "黄豆",
                price: 6,
                stock: 900
            },
        ];

        let nameArr = [];

        let str = "";

        let timmer = null;

        data.forEach(item => {
            str += `<tr>
                        <td>${item.name}</td>
                        <td>${item.price}元/斤</td>
                        <td>${item.stock}斤</td>
                    </tr>`
        })

        $("tbody").innerHTML = str;

        $("input").oninput = () => {
            str = "";
            if (timmer) {
                clearTimeout(timmer);
            }
            
            timmer = setTimeout(() => {
                let str1 = $("input").value;

                let newData = data.filter(item => item.name.indexOf(str1) != -1 ? item : false);

                $("tbody").innerHTML = '';
                if (newData == 0) {
                    str += `<tr >
                                <td colspan="3" class="noneData">暂无数据！</td>
                            </tr>`
                } else {
                    newData.forEach(item => {
                        str += `<tr>
                                <td>${item.name}</td>
                                <td>${item.price}元/斤</td>
                                <td>${item.stock}斤</td>
                            </tr>`
                    })
                }
                $("tbody").innerHTML = str;
            }, 500)
        }
    </script>
</body>

</html>